package com.len.util;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class SMSUtil {

    public static String sendSMS(String appkey, String secretkey, String phone, String content) throws Exception {
        Map<String,String> params = new HashMap<>();
        params.put("appkey", appkey);
        params.put("secretkey", secretkey);
        params.put("phone", phone);
        params.put("content", URLEncoder.encode(content, "UTF-8"));
        return HttpUtil.post("http://api2.nxcloud.com/api/sms/mtsend",params,"UTF-8");
    }



    public static String specialUrlEncode(String value) throws Exception {
        return java.net.URLEncoder.encode(value, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
    }

    public static String sign(String accessSecret, String stringToSign) throws Exception {
        javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA1");
        mac.init(new javax.crypto.spec.SecretKeySpec(accessSecret.getBytes("UTF-8"), "HmacSHA1"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        return new sun.misc.BASE64Encoder().encode(signData);
    }
}
